//
//  AudioFaceViewController.m
//  TestAispeech
//
//  Created by shaowei on 13-9-7.
//  Copyright (c) 2013年 LianZhan. All rights reserved.
//

#import "AudioFaceViewController.h"
#import "AudioManager.h"
#import "FaceManager.h"
#import "AudioVerifyPwdViewController.h"
#import "FaceVerifyViewController.h"
#import "AudioSetPwdViewController.h"
#import "FaceSetPwdViewController.h"

@interface AudioFaceViewController ()

@end

@implementation AudioFaceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化按钮
    [self setStateOfBtn:VerifyBtnStateOfAudio];
    
    //检测本地已经保存的声音与人面密码
    [self checkAllPwdState];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    [_btnOfVerify release];
    [_labelOfResult release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setBtnOfVerify:nil];
    [self setLabelOfResult:nil];
    [super viewDidUnload];
}

- (void)setStateOfBtn:(VerifyBtnState)stateOfBtn{
    _stateOfBtn = stateOfBtn;
    
    switch (_stateOfBtn) {
        case VerifyBtnStateOfAudio:{
            [self.btnOfVerify setTitle:@"点击进行 声音密码验证" forState:UIControlStateNormal];
            self.labelOfResult.text = @"还未开始验证;\n点击上面按钮进行声音验证。";
        }
            break;
        case VerifyBtnStateOfFace:{
            [self.btnOfVerify setTitle:@"点击进行 人脸密码验证" forState:UIControlStateNormal];
            self.labelOfResult.text = @"声音验证通过；\n点击上面按钮进行人脸识别验证。";
        }
            break;
        case VerifyBtnStateOfFinish:{
            [self.btnOfVerify setTitle:@"验证完成" forState:UIControlStateNormal];
            [self.btnOfVerify setEnabled:NO];
            self.labelOfResult.text = @"声音验证通过；\n人脸识别验证通过；\n恭喜您全部通过验证";
        }
            break;
            
        default:
            break;
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    __block AudioFaceViewController *tempSelf = self;
    if ([segue.identifier isEqualToString:@"audio_verify"]) {
        AudioVerifyPwdViewController *tempVC = (AudioVerifyPwdViewController *)segue.destinationViewController;
        [tempVC setCompleteHandle:^(BOOL result) {
            if (result) {
                //回到前台主线程执行
                dispatch_async(dispatch_get_main_queue(), ^{
                    [tempSelf setStateOfBtn:VerifyBtnStateOfFace];
                    [tempSelf.presentedViewController  dismissModalViewControllerAnimated:YES];
                }) ;
            }
        }];
    }else if ([segue.identifier isEqualToString:@"face_verify"]) {
        FaceVerifyViewController *tempVC = (FaceVerifyViewController *)segue.destinationViewController;
        [tempVC setCompleteHandle:^(BOOL result) {
            if (result) {
                //回到前台主线程执行
                dispatch_async(dispatch_get_main_queue(), ^{
                    [tempSelf setStateOfBtn:VerifyBtnStateOfFinish];
                    [tempSelf.presentedViewController  dismissModalViewControllerAnimated:YES];
                }) ;
            }
        }];
    }else if ([segue.identifier isEqualToString:@"audio_set"]){
        AudioSetPwdViewController *tempVC = (AudioSetPwdViewController *)segue.destinationViewController;
        [tempVC setCompleteHandle:^(BOOL success) {
            if (success) {
                //回到前台主线程执行
                dispatch_async(dispatch_get_main_queue(), ^{
                    [tempSelf.presentedViewController  dismissModalViewControllerAnimated:YES];
                    [self checkFacePwdState];
                }) ;
                
            }
        }];
    }else if ([segue.identifier isEqualToString:@"face_set"]){
        FaceSetPwdViewController *tempVC = (FaceSetPwdViewController *)segue.destinationViewController;
        [tempVC setCompleteBlock:^(PersonModel * personInfo,BOOL success) {
            if (success) {
                //回到前台主线程执行
                dispatch_async(dispatch_get_main_queue(), ^{
                    [tempSelf.presentedViewController  dismissModalViewControllerAnimated:YES];
                }) ;
                
            }
        }];
    }
}

#pragma mark Function - Custom

- (void)checkAllPwdState{
    if ([self checkAudioPwdState]) {
        [self checkFacePwdState];
    }
}

- (BOOL)checkAudioPwdState{
    BOOL result = [AudioManager isHaveAudioPwd];
    if (!result) {
        ALERT_VIEW(@"您还未设置过声音密码", @"开始设置声音密码");
        [self performSegueWithIdentifier:@"audio_set" sender:nil];
    }
    return result;
}

- (BOOL)checkFacePwdState{
    BOOL result = [FaceManager isHavePersonInfo];
    if (!result) {
        ALERT_VIEW(@"您还未设置过人脸密码", @"开始设置人脸密码");
        [self performSegueWithIdentifier:@"face_set" sender:nil];
    }
    
    return result;
}



- (IBAction)actionVerify:(id)sender {
    switch (_stateOfBtn) {
        case VerifyBtnStateOfAudio:{
            [self performSegueWithIdentifier:@"audio_verify" sender:nil];
        }
            break;
        case VerifyBtnStateOfFace:{
            [self performSegueWithIdentifier:@"face_verify" sender:nil];
        }
            break;
        case VerifyBtnStateOfFinish:{
            
        }
            break;
            
        default:
            break;
    }
}

@end
